// 使用async await 解决回调地狱问题

const { assert } = require("console");
const fs = require("fs");

function getFileData(filename) {
    return new Promise((resolve, reject) => {
        fs.readFile(filename, 'utf-8', (err, data) => {
            if (err) {
                // 读取失败
                reject(err.message) // promise实例对象状态 pending  变成  rejected  状态标记成失败
                return console.log(`读取失败 ${err.message}`)
            }

            // 读取成功
            resolve(data) // promise实例对象状态 pending  变成  fulfilled  状态标记成功
        })
    })
}

let p1 = getFileData('./files/1.txt')
let p2 = getFileData('./files/2.txt')
let p3 = getFileData('./files/3.txt')

async function fn(){
    const res1 = await p1
    console.log(res1);

    const res2 = await p2
    console.log(res2);
    
    const res3 = await p3
    console.log(res3);
}

fn()